package br.edu.unicentro.mymib.entity;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.MappedSuperclass;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import br.edu.unicentro.mymib.base.EMFactory;
import br.edu.unicentro.mymib.base.EntityManagerService;

@Entity
@DiscriminatorValue("Administrador")
@MappedSuperclass
@NamedQueries( { @NamedQuery(name = "admin.find", query = "from Administrador as a where a.email = :email") })
public final class Administrador extends Pessoa {

	public Administrador() {

	}

	public void removerUsuario(Usuario usuario) throws Exception {
		EntityManagerService.remover(usuario);
	}

	public static Administrador buscar() {
		EntityManager em = EMFactory.getManager();
		Query query = em.createNamedQuery("admin.find");
		query.setParameter("email", "admin");
		try {
			return (Administrador) query.getSingleResult();
		} catch (NoResultException e) {
			Administrador adm = new Administrador();

			adm.setEmail("admin");
			adm.setNome("admin");
			adm.setSenha("admin");

			try {
				return (Administrador) EntityManagerService.salvar(adm);
			} catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}

		}
	}

}
